package com.hellobike.haxing.modules.supplier.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hellobike.haxing.modules.address.entity.TKV;
import com.hellobike.haxing.modules.address.util.Convert2KV;
import com.hellobike.haxing.modules.product.entity.Category;
import com.hellobike.haxing.modules.product.mapper.CategoryMapper;
import com.hellobike.haxing.modules.supplier.entity.TSupplierClassification;
import com.hellobike.haxing.modules.supplier.mapper.TSupplierClassificationMapper;
import com.hellobike.haxing.modules.supplier.service.ITSupplierClassificationService;
import com.hellobike.haxing.modules.wms.entity.Warehouse;
import com.hellobike.haxing.modules.wms.mapper.WarehouseMapper;
import com.hellobike.haxing.modules.wms.utils.ListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

/**
 * @Description: t_supplier_classification
 * @Author: haxing-boot
 * @Date:   2020-01-14
 * @Version: V1.0
 */
@Service
public class TSupplierClassificationServiceImpl extends ServiceImpl<TSupplierClassificationMapper, TSupplierClassification> implements ITSupplierClassificationService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private WarehouseMapper warehouseMapper;

    @Autowired
    private TSupplierClassificationMapper tSupplierClassificationMapper;

    @Override
    public String getCategoryNames(List<String> cateids) {
        if (cateids != null && cateids.size() > 0) {
            List<Category> categoryList = categoryMapper.selectBatchIds(cateids);
            if(categoryList == null){
                return null;
            }
            List<Category> categories = categoryList.stream().collect(collectingAndThen(
                    toCollection(() -> new TreeSet<>(comparing(n->n.getId()))), ArrayList::new)
            );
            List<String> categoryStr = categories.stream().map(Category::getName).collect(Collectors.toList());

            return categoryStr.stream().collect(Collectors.joining(","));

        }
        return null;
    }

    @Override
    public List<TKV> getCategorys(List<String> cateids) {
        if (cateids != null && cateids.size() > 0) {
            QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("parent_id", cateids);
            List<Category> categoryList = categoryMapper.selectList(queryWrapper);
            if(categoryList == null){
                return null;
            }
            List<Category> categories = categoryList.stream().collect(collectingAndThen(
                    toCollection(() -> new TreeSet<>(comparing(n->n.getId()))), ArrayList::new)
            );

            return Convert2KV.Category2kv(categories);

        }
        return null;
    }

    @Override
    public List<TKV> getWareHouse(List<String> cityIds){
        QueryWrapper<Warehouse> queryWrapper = new QueryWrapper<>();
        if(cityIds != null && cityIds.size() > 0) {
            queryWrapper.in("city_id", cityIds);
        }
        List<Warehouse> warehouseList = warehouseMapper.selectList(queryWrapper);
        if(warehouseList == null){
            return null;
        }
        List<Warehouse> warehouses = warehouseList.stream().collect(collectingAndThen(
                toCollection(() -> new TreeSet<>(comparing(n->n.getId()))), ArrayList::new)
        );
        return Convert2KV.WareHouse2kv(warehouses);

    }

    @Override
    public List<TKV> getSupplierInfoByWarehouseIds(String warehouseId){
        QueryWrapper<TSupplierClassification> queryWrapper = new QueryWrapper<>();
        if(warehouseId != null ) {
            queryWrapper.like("warehouse", warehouseId);
        }
        List<TSupplierClassification> tSupplierClassificationList = tSupplierClassificationMapper.selectList(queryWrapper);
        if(tSupplierClassificationList == null){
            return null;
        }
        List<TSupplierClassification> warehouses = tSupplierClassificationList.stream().collect(collectingAndThen(
                toCollection(() -> new TreeSet<>(comparing(n->n.getId()))), ArrayList::new)
        );
        return Convert2KV.SupplierClassT2kv(warehouses);

    }


    @Override
    public String getWareHouseNames(List<String> ids){
        QueryWrapper<Warehouse> queryWrapper = new QueryWrapper<>();
        if(ListUtil.isNullOrEmpty(ids)) {
            return null;
        }else{
            queryWrapper.in("id", ids);
        }
        List<Warehouse> warehouseList = warehouseMapper.selectList(queryWrapper);
        if(ListUtil.isNullOrEmpty(warehouseList)){
            return null;
        }
        List<Warehouse> warehouses = warehouseList.stream().collect(collectingAndThen(
                toCollection(() -> new TreeSet<>(comparing(n->n.getId()))), ArrayList::new)
        );
        List<String> warehouseNames=warehouses.stream().map(Warehouse::getWarehouseName).collect(Collectors.toList());

        return String.join(",",warehouseNames);

    }

}
